package easytalk.views.custom;


import java.io.File;
import java.io.FilenameFilter;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;

public class SmileyDialog extends Dialog{

	public SmileyDialog(Shell shell) {
		super(shell);
		setShellStyle(SWT.MODELESS | SWT.CLOSE);
		shell.addFocusListener(new FocusAdapter() {
			
			@Override
			public void focusLost(FocusEvent e) {			
				closeWindow();
			}
			
		});
	}
	
	@Override
	protected Control createDialogArea(Composite parent) {
		
		
		Control control = super.createDialogArea(parent);
		
		parent.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {			
				closeWindow();
			}
			
		});
		
		GridLayout gridLayout = new GridLayout(4, true);
		gridLayout.marginWidth = 5;
		gridLayout.marginHeight = 5;
		gridLayout.verticalSpacing = 0;
		gridLayout.horizontalSpacing = 0;
		parent.setLayout(gridLayout);
		
		File file = new File(Messages.getString("SmileyDialog.0")); //$NON-NLS-1$
		
		System.out.println(file.getAbsolutePath());
		File childFiles[] = file.listFiles(new FilenameFilter() {

			@Override
			public boolean accept(File dir, String name) {			
				return name.endsWith(Messages.getString("SmileyDialog.1")); //$NON-NLS-1$
			}
			
		});
		int size = childFiles.length;
		
		Image images[] = new Image[size];
		
		for (int i = 0; i < childFiles.length; i++) {
			
			Button button = new Button(parent, SWT.PUSH);
			button.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false,
					false));
		
			ImageData data = new ImageData(childFiles[i].getAbsolutePath()).scaledTo(32, 32);
			images[i] = new Image(parent.getDisplay(),data);

			button.setImage(images[i]);
			
			button.addSelectionListener(new SelectionAdapter() {
				@Override
				public void widgetSelected(SelectionEvent e) {
				
					closeWindow();
				}
			});
		
		}
		
		return control;
	}

	@Override
	protected Control createButtonBar(Composite parent) {
		// TODO Auto-generated method stub
		return null;
	}
	
	private void closeWindow() {
		close();

	}
	
	
}
